package com.sleep.psy.external_server.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author wenliguo
 * @date 2025年06月10日 10:50
 */
public class AudioUtils {

    protected static Logger logger = LoggerFactory.getLogger(AudioUtils.class);

    /**
     * 合并音频
     * @author wenliguo
     * @date 2025/6/10 10:56
     * @param filePaths 原始文件
     * @param combinePath 合并后的新文件
    */
    public static Integer combine(String commandFix, List<String> filePaths, String combinePath) {
       try {

           StringBuilder command = new StringBuilder(commandFix + " -i \"concat:");
           for (String path : filePaths) {
               command.append(path).append("|");
           }
           command.append("\" -acodec copy ").append(combinePath).append(" -y");

           logger.info("执行合并音频代码--" + command);

           //拼接ffmpeg命令
           Process process = null;
           Thread.sleep(1000);
           //执行本地命令
           Runtime r = Runtime.getRuntime();

           String[] cmd = new String[]{"sh", "-c", command.toString()};

           process = r.exec(cmd);
           return process.waitFor();
       } catch (Exception e)  {
           logger.error("FFmpeg 执行失败，错误码",e);
           return -1;
       }

    }
}
